

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.DropMode;
import java.awt.Color;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.text.ParseException;
import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.Iterator;

import javax.swing.JOptionPane;
import java.awt.Font;
import javax.swing.JFormattedTextField;
import javax.swing.text.MaskFormatter;
import javax.swing.JSeparator;
import javax.swing.JComboBox;
import javax.swing.JSpinner;
import javax.swing.JList;
import javax.swing.JToggleButton;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

public class VueComptePerso extends Vue {

	/**
	 * 
	 */
	//private static final long serialVersionUID = 5069349224425178639L;
	private JPanel contentPane;
	private JTextField textFieldNom;
	private JTextField textFieldPrenom;
	
	private JButton btnEnregistrer;
	private JButton btnAnnuler;
	private JButton btnFermer;
	private ClientPhysique _client ;
	private JSeparator separator;
	private JLabel lblChoisirUnCompte;
	private JComboBox listeCompte;
	private final JLabel lblCompte = new JLabel("Numéro de Compte :");
	private JLabel lblSolde;
	private JLabel lbSolde;
	private JLabel lbType;
	private JLabel lbDecouvert; 
	private JLabel lbNum;
	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public VueComptePerso(Controleur controleur) throws ParseException {
		super (controleur);
		setTitle("Consulter un compte Perso");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 600, 397);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNom = new JLabel("Nom :");
		lblNom.setBounds(41, 38, 38, 16);
		contentPane.add(lblNom);
		
		textFieldNom = new JTextField();
		textFieldNom.setBounds(87, 33, 155, 28);
		textFieldNom.setBackground(new Color(255, 255, 255));
		contentPane.add(textFieldNom);
		textFieldNom.setColumns(10);
		
		btnEnregistrer = new JButton("Rechercher");
		btnEnregistrer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String nom = textFieldNom.getText().trim().toLowerCase();
				String prenom = textFieldPrenom.getText().trim().toLowerCase();
				getControleur().getComptePerso(getControleur().rechClientPhysique(nom+prenom));
			}
		});
		btnEnregistrer.setBounds(310, 73, 117, 29);
		contentPane.add(btnEnregistrer);
		
		btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueComptePerso.this);
			}
		});
		btnAnnuler.setBounds(160, 73, 117, 29);
		contentPane.add(btnAnnuler);
		
		JLabel lblPrnom = new JLabel("Prénom :");
		lblPrnom.setBounds(322, 38, 61, 16);
		contentPane.add(lblPrnom);
		
		textFieldPrenom = new JTextField();
		textFieldPrenom.setBounds(392, 33, 155, 28);
		contentPane.add(textFieldPrenom);
		textFieldPrenom.setColumns(10);
		
		separator = new JSeparator();
		separator.setBounds(12, 114, 573, 3);
		contentPane.add(separator);
		
		lblChoisirUnCompte = new JLabel("Choisir un Compte");
		lblChoisirUnCompte.setBounds(70, 132, 123, 15);
		contentPane.add(lblChoisirUnCompte);
		
		listeCompte = new JComboBox();
		listeCompte.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent e) {
				ComptePerso cpt = (ComptePerso) listeCompte.getSelectedItem();
				afficheInfos(cpt);
			}
		});
		listeCompte.setBounds(211, 129, 177, 24);
		contentPane.add(listeCompte);
		lblCompte.setBounds(310, 197, 131, 31);
		contentPane.add(lblCompte);
		
		JLabel lblTypeDeCompte = new JLabel("Type de Compte :");
		lblTypeDeCompte.setBounds(38, 205, 120, 15);
		contentPane.add(lblTypeDeCompte);
		
		lblSolde = new JLabel("Solde :");
		lblSolde.setBounds(41, 299, 61, 15);
		contentPane.add(lblSolde);
		
		JLabel lblDecouvertAutoris = new JLabel("Decouvert Autorisé :");
		lblDecouvertAutoris.setBounds(310, 299, 131, 15);
		contentPane.add(lblDecouvertAutoris);
		
		btnFermer = new JButton("Fermer");
		btnFermer.setBounds(244, 336, 107, 25);
		contentPane.add(btnFermer);
		btnFermer.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().fermerVue(VueComptePerso.this);
			}
		});
		lbSolde = new JLabel("");
		lbSolde.setFont(new Font("Dialog", Font.BOLD, 12));
		lbSolde.setBounds(165, 299, 61, 15);
		contentPane.add(lbSolde);
		
		lbType = new JLabel("");
		lbType.setBounds(162, 205, 61, 15);
		contentPane.add(lbType);
		
		lbNum = new JLabel("");
		lbNum.setBounds(454, 205, 61, 15);
		contentPane.add(lbNum);
		
		lbDecouvert = new JLabel("");
		lbDecouvert.setBounds(457, 298, 61, 15);
		contentPane.add(lbDecouvert);
	}
	
	public void setEtat (int etat){
		switch (etat) {
		case initiale: {
			btnEnregistrer.setEnabled(true);
			btnAnnuler.setEnabled(true);
			break;
			}
		}
	}
	
	public void afficheInfos(ComptePerso cpt){
		lbNum.setText(String.valueOf(cpt.getNumero()));
		lbType.setText(cpt.getType());
		lbSolde.setText(cpt.getSolde().toString());
		lbDecouvert.setText(String.valueOf(cpt.getDecouvertAutorise()));
		if(cpt.getSolde() < 0){
			lbSolde.setForeground(Color.RED);
		}
		else{
			lbSolde.setForeground(Color.GREEN);
		}
	}
	
	public void aliment(HashSet<ComptePerso> cpt) {
		Iterator<ComptePerso> it = cpt.iterator();
		while (it.hasNext()) {
			listeCompte.addItem(it.next());
		}		
	}
	
 
}
